package com.nowcoder.community.entity;

/**
 * 封装分页相关的信息
 */
public class Page {
    private int current=1;//当前页码
    private int limit=10;//每页显示多少数据
    private int rows;//总共有多少条数据
    private int showPages=5;//下面的页码一次显示5页
    private String path;

    public int getShowPages() {
        return showPages;
    }

    public void setShowPages(int showPages) {
        if(showPages<=20 && showPages>=1) {
            this.showPages = showPages;
        }
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        if(rows>=0) {
            this.rows = rows;
        }
    }

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        if(current>=1) {
            this.current = current;
        }
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        if(limit>=1 && limit<=100) {
            this.limit = limit;
        }
    }

    /**
     * 获取总共有多少页
     */
    public int getTotal(){
        if(rows%limit==0)
            return rows/limit;
        else
            return rows/limit+1;
    }

    //页面上要显示从几页到几页
    /**
     * 获取起始显示页
     */
    public int getStartPage(){
        int start = current-2;
        //如果current是最后两页，前面就多显示几页
        int total=getTotal();
        if(total-start<=showPages-1)
            start=total-showPages+1;

        return start<1?1:start;
    }
    /**
     * 获取终止显示页
     */
    public int getEndPage(){
        int end = current+2;
        //如果current是最前面两页，后面就多显示几页
        end=end<showPages?showPages:end;

        int total=getTotal();
        return end>total?total:end;
    }

    /**
     * 起始查询行
     */
    public int getStartRow(){
        return (current-1)*limit;
    }
}